package com.eso.frame.handler;

import com.eso.frame.ComponentManager;
import com.eso.frame.task.ISpringContextTask;
import com.eso.frame.task.TaskException;
import com.eso.frame.task.job.RunningJobManager;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 定时任务加载数据辅助实现类
 * User: Noah.Rong
 * Version: 0.1,  2012-05-22 16:58
 */
@Component
public class ReloadDataHandler implements IReloadDataHandler {

    @Resource(name = "executingJobsManager")
    private RunningJobManager runningJobManager;

    @Override
    public Boolean isJobAutoRunning(String taskIdentity) {
        final Map beanMap = ComponentManager.getInstance().getBeansOfType(ISpringContextTask.class);
        ISpringContextTask springContextTask = (ISpringContextTask) beanMap.get(taskIdentity);
        return runningJobManager.isRunning(springContextTask);
    }

    @Override
    public void executeReloadDataTask(String taskIdentity) throws TaskException {
        final Map beanMap = ComponentManager.getInstance().getBeansOfType(ISpringContextTask.class);
        if (beanMap.containsKey(taskIdentity)) {
            String task = taskIdentity;
            ISpringContextTask springContextTask = (ISpringContextTask) beanMap.get(task);
            springContextTask.executeTaskByHand();
        }
    }
}
